package com.xpgk.mvc.collection.measurement;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.tools.ToolRandoms;

public class CollectionMeasurementController extends BaseController {

	private CollectionTemporaryServices collectionTemporaryServices = new CollectionTemporaryServices();

	public void index() {
		
		// 获取当前机构id
		String organids = getCUserOrganIds();
		String userids = getCUserIds();
		setAttr("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		setAttr("organids", organids);
		setAttr("userids", userids);
		setAttr("customerIds", ToolRandoms.getUuid(true));
		List<Record> productlist = Db.find("SELECT * FROM sb_product_price WHERE priceIds=? and productName in ('单疗程','标准疗程','一疗程带售后') and status ='1'",Organization.dao.findById(organids).getStr(Organization.column_priceIds));
		setAttr("productlist", productlist);
		render("colConsultMesure.html");
	}



	/**
	 * 测算
	 */
	@Before({ CollectionTemporaryValidator.class, Tx.class })
	public void cesuan() {
		// 获取当前人员所属机构ids
		String organids = getCUserOrganIds();
		CollectionTemporary temp = getModel(CollectionTemporary.class, "temporary");
		if (temp != null) {
			setSessionAttr(CollectionTemporary.column_ids + temp.getInt("ids"), temp);
		}
		temp.set(CollectionTemporary.column_gkfl, Code.cjCus_ids);
		Map<String, Object> mm = collectionTemporaryServices.saveRecord(temp, organids);
		renderJson(mm);
	}

	/**
	 * 获取介绍人门店
	 * 
	 */

	public void getJsrStore() {
		String jsrPhone = getPara("jsrPhone");
		RenderBean result = collectionTemporaryServices.getJsrStore(jsrPhone);
		renderJson(result);

	}
	

	/**
	 * 保存
	 */
	@Before({CollectionTemporaryValidator.class, Tx.class })
	public void saveTemp() {
		// 获取当前登录机构id
		String cudid = getCUserOrganIds();

		String organname =Organization.dao.getOrganizationInfo(cudid).getStr(Organization.column_name);

		String userIds = getCUserIds();
		CollectionTemporary temp = getModel(CollectionTemporary.class, "temporary");
		
		CustomerAgreement cusAgr = getModel(CustomerAgreement.class,"customerAgreement");
		
		CustomerDetail cusdet = getModel(CustomerDetail.class,"customerDetail");
		
		
		
		
		if(cusAgr!=null){
			if(StringUtils.isNotBlank(cusAgr.getStr("codeIds"))&&cusAgr.getStr("codeIds").contains(",")){
				if(cusAgr.getInt("treatment")>0){
					cusAgr.set("cost",  Double.parseDouble(cusAgr.getStr("codeIds").split(",")[1])*cusAgr.getInt("treatment"));
				}
				cusAgr.set("codeIds", cusAgr.getStr("codeIds").split(",")[0]);
			}
		}
		String delWeight = getPara("delWeight");
		String targetWeight = getPara("targetWeight");
		String chargePersion = getPara("chargePersion");
		String customerIds = getPara("customerIds");
		temp .set(CollectionTemporary.column_gkfl, Code.cjCus_ids);
		
		
		// 获取当月
		SimpleDateFormat df = new SimpleDateFormat("yyMM");
		String date = df.format(temp.getDate(CollectionTemporary.column_gxsj));
		// 获取当前代理商门店编号
		Record r=Db.findFirst("SELECT CASE WHEN  number  is null  THEN  0 ELSE  substring(number,5)  END  n    from sb_customer_temporary WHERE organIds =?  ORDER BY   substring(number,5) DESC LIMIT 1",cudid);
		int a =Integer.parseInt(r.getStr("n"));
		String s= date + String.format("%03d", a + 1);
		temp.set(CollectionTemporary.column_number, s);
		RenderBean result = collectionTemporaryServices.saveTempAndCus(organname, temp, cusAgr, delWeight, targetWeight, cudid,chargePersion, userIds,customerIds,cusdet);
		renderJson(result.getMap());
	}

	/**
	 * 用手机号搜索顾客测算信息
	 */
	public void phoneSearch() {
		String phone = getPara("phone");
		String name = getPara("name");
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		Map<String, Object> mm = collectionTemporaryServices.phoneSearch(name, phone, orgid);
		renderJson(mm);
	}

	/**
	 * 获取会员名字/id
	 * 
	 */
	public void getCusInfo() {
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		List<CollectionTemporary> mm = collectionTemporaryServices.getCusInfo(orgid);
		renderJson(mm);
	}
	
	/**
	 * 获取会员及用户信息/id
	 * 
	 * */
	public void  getCusAndUserInfo(){
		List<CollectionTemporary>  mm=collectionTemporaryServices.getCusAndUserInfo();
		renderJson(mm);
	}
	
	@Before(Tx.class)
	public void initimport(){
	// 获取文件
	UploadFile file = getFile("filePath");
	String organIds  = getCUserOrganIds();
	String userIds = getCUserIds();
	Map<String, Object> mm = collectionTemporaryServices.imageUpload(file,organIds,userIds);
	renderJson(mm);
	}

}
